package com.qf.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.http.Result;
import com.qf.dto.MerchantUser;

import com.qf.service.impl.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("merchant")
public class MerchantController {

    @Resource
    private MerchantService merchantService;

    @RequestMapping("query")
    public String toList(Model model, @RequestParam(required = false) String query) {
        QueryWrapper<MerchantUser> wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(query)) {
            wrapper.lambda().like(MerchantUser::getName, query).or().like(MerchantUser::getUserName, query);
        }
        wrapper.lambda().orderByAsc(MerchantUser::getCreateTime);
        model.addAttribute("merchantUserList", merchantService.list(wrapper));
        return "merchant/merchant";
    }

    @PostMapping("getById")
    @ResponseBody
    public Result<MerchantUser> getById(@RequestParam(value = "id", required = true) Long id) {
        return Result.success(merchantService.getById(id));
    }

    @PostMapping("save")
    @ResponseBody
    public Result<?> save(MerchantUser merchantUser) {
        merchantService.saveOrUpdate(merchantUser);
        return Result.success();
    }

    @PostMapping("del")
    @ResponseBody
    public Result<?> del(@RequestParam(value = "id", required = true) Long id) {
        merchantService.removeById(id);
        return Result.success();
    }

}